﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;


namespace HR60_PanelSoft.View.TabForms.ListItems
{
	public partial class SceneAttrList : BaseFormControl
	{
		private Scene scene = null;

		private Device device = null;
		
		public SceneAttrList()
		{
			InitializeComponent();
		}

		// 目标场景
		public Scene TagScene
		{
			get
			{
				return scene;
			}
		}

		// 目标设备
		public Device TagDevice
		{
			get
			{
				return device;
			}
		}

		// 初始化界面
		public void init(Device device, Scene scene)
		{
			this.device = device;
			this.scene = scene;
			SceneEvent se = scene.findEventByDevice(device.uuid);

			ltlName.Text = device.nickName;
			tblList.RowCount = device.type.attrs.Count + 1;

			foreach (CtrlAttr attr in device.type.attrs)
			{
				SceneAttrItem item = new SceneAttrItem(attr);
				item.AutoSubmitFlag = false;
				tblList.Controls.Add(item);

				if (se != null && se.attrs.IndexOf(attr.name) > -1)
				{
					item.Value = se.attrValueKeys[se.attrs.IndexOf(attr.name)];
				}

				item.ValueChanged += new EventHandler(valueChange);
			}
		}

		// 重写Value属性，返回SceneEvent对象，只读
		public new object Value
		{
			get
			{
				SceneEvent se = new SceneEvent();
				se.device = device.uuid;
				for (int i = 1; i < tblList.Controls.Count; i++)
				{
					SceneAttrItem item = (SceneAttrItem) tblList.Controls[i];
					string valueKey = (string)item.Value;
					if (valueKey != null)
					{
						se.attrs.Add(item.TagAttr.name);
						se.attrValueKeys.Add(valueKey);
					}
				}
				return se.attrs.Count == 0 ? null : se;
			}
		}
	}
}
